/**
 * fileName: YLES_SEARCH_TOOL
 * @NApiVersion 2.1
 */
define(["require", "exports", "N/search", "N/log", "N/record"], function (require, exports, search, log, record) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.getCsvFolder = exports.getCsvParentFolder = exports.getAllItem = exports.dealPageSearch = exports.dealSearch = void 0;
    //处理Search所有数据
    function dealSearch(Search, cb, obj) {
        const myPagedData = Search.runPaged({ pageSize: 1000 });
        myPagedData.pageRanges.forEach(function (pageRange) {
            const myPage = myPagedData.fetch({ index: pageRange.index });
            myPage.data.forEach(function (result) {
                cb(result, Search.columns, obj);
            });
        });
    }
    exports.dealSearch = dealSearch;
    //处理Search数据。返回指定页数数据
    function dealPageSearch(Search, cb, page, pageSize, obj) {
        const myPagedData = Search.runPaged({ pageSize: pageSize });
        obj.counts = myPagedData.count;
        if (obj.counts > 0) {
            const PageDate = myPagedData.fetch({ index: page }).data;
            PageDate.forEach(function (result) {
                cb(result, Search.columns, obj);
            });
        }
    }
    exports.dealPageSearch = dealPageSearch;
    //获取所有货品主数据
    function getAllItem() {
        const rtn = {
            "itemInfo": []
        };
        const Search = search.create({
            type: 'item',
            filters: [
                ['isinactive', 'is', 'F']
            ],
            columns: [
                { name: 'itemid' }
            ]
        });
        let cb = (res, columns, obj) => {
            obj.itemInfo.push(res.getValue(columns[0]));
        };
        dealSearch(Search, cb, rtn);
        return rtn.itemInfo;
    }
    exports.getAllItem = getAllItem;
    //获取存储CSV的顶层文件夹ID
    function getCsvParentFolder() {
        const filters = [];
        const columns = [];
        filters.push(['name', 'is', 'yles_csv_folder']);
        columns.push(search.createColumn({ name: 'internalid' }));
        const thisSearch = search.create({ type: 'folder', filters: filters, columns: columns });
        const thisResult = thisSearch.run().getRange({ start: 0, end: 1 });
        if (thisResult.length > 0) {
            return thisResult[0].getValue(columns[0]);
        }
        throw 'Please contact the administrator to create the report storage folder first.';
    }
    exports.getCsvParentFolder = getCsvParentFolder;
    //获取指定名称的文件夹ID
    function getCsvFolder() {
        const now = new Date();
        const folderName = 'ibCsv_' + now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate();
        //1.获取parent folder
        const pFolderId = getCsvParentFolder();
        //2.获取指定名称且在CSV父级目录下的文件夹，若不存在则新建
        const filters = [];
        var columns = [];
        filters.push(['name', 'is', folderName]);
        filters.push('and');
        filters.push(['parent', 'is', pFolderId]);
        columns.push(search.createColumn({ name: 'internalid' }));
        var thisSearch = search.create({ type: 'folder', filters: filters, columns: columns });
        var thisResult = thisSearch.run().getRange({ start: 0, end: 10 });
        if (thisResult && thisResult.length > 0) {
            return thisResult[0].getValue(columns[0]);
        }
        else {
            try {
                //若不存在，则新建
                const folderRecord = record.create({ type: 'folder' });
                folderRecord.setValue('name', folderName);
                folderRecord.setValue('parent', pFolderId);
                return folderRecord.save();
            }
            catch (e) {
                log.error({ title: '创建文件夹失败', details: e });
            }
        }
        return '';
    }
    exports.getCsvFolder = getCsvFolder;
});
